package com.dream.service2.utils;

import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

public class Spelparser {

    private static ExpressionParser parser = new SpelExpressionParser();
    public static String getKey(String key,String[] parameterNames,Object[] args){
        //1.将key字符串解析为el表达式
        Expression expression = parser.parseExpression(key);
        //2.将形参和形参值以配对的方式配置到赋值上下文中
        StandardEvaluationContext context = new StandardEvaluationContext();
        if (args.length<0){
            return null;
        }
        for (int i=0;i<args.length;i++){
            context.setVariable(parameterNames[i],args[i]);
        }
        //3.根据赋值的上下文运算el表达式
        return expression.getValue(context,String.class);
    }
}